import { RouteObject } from "react-router-dom";

/**
 * "/a/b" => ['/a', '/a/b']
 */
export const genPaths = (path: string) => {
  const tmp = path.split("/").slice(1); // ['dashboard', 'monitor']
  return tmp.reduce((prev: string[], item: string, index: number) => {
    if (index === 0) {
      prev.push(`/${item}`);
    } else {
      prev.push(`${prev[index - 1]}/${item}`);
    }

    return prev;
  }, []);
};

/**
 *
 */
export const genKeyLabelMap = (routes: RouteObject[], result: any) => {
  routes.forEach((route) => {
    result[route.key] = route.label;

    // 判断是否有children
    if (route.children?.length) {
      genKeyLabelMap(route.children, result);
    }
  });

  return result;
};
